using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class SiteDal : Base
    {
        public SiteDal() : base() { }

        public enum Fields
        {
            Id,
            Name,
            Root
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<string>(Fields.Root, reader, table);
        }

        public Hashtable[] GetAll()
        {
            SqlCommand cmd = CreateCommand("GetAll");
            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByUrl(string siteUrl)
        {
            SqlCommand cmd = CreateCommand("GetByUrl");
            AddInputParameter(cmd, "Url", siteUrl, SqlDbType.VarChar, 255);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByRootUrl(string rootUrl)
        {
            SqlCommand cmd = CreateCommand("GetByRootUrl");
            AddInputParameter(cmd, "RootUrl", rootUrl, SqlDbType.VarChar, 255);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByName(string name)
        {
            SqlCommand cmd = CreateCommand("GetByName");
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 50);
            return ExecuteGetItem(cmd);
        }

        public void Update(ref int id, string name, string root, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 20);
            AddInputParameter(cmd, "Root", root, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "VersionIdOut", SqlDbType.Timestamp);
            ExecuteCommand(cmd);
            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "VersionIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }



    }
}
